﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Reflection;
using ZebraCoreLib;
using ZebraCoreLib.BasBiz;
using ZebraCoreLib.Framework;

namespace Web.ZebraUIControls
{
    public class ZebraDataList:ZebraBasDataList
    {

        protected Dictionary<string, string> ListItem;
       
        public ListFlags ListFlag{get;set;}
        //public int PageIndex { get; set; }
        //public int PageSize { get; set; }
        //public int PageCount { get; set; }
        public string UserControlFileName { get; set; }

        public ZebraPager Pager { get; set; }



        public ZebraDataList()
        {
            //PageIndex = 1;
            //PageCount = 0;
            //PageSize = 15;
            ListFlag = ListFlags.Master;

        }
        public string ServiceMethod { get; set; }

        protected string bizServiceClass
        {
            get
            {
                string bizClass = "";
                if (!string.IsNullOrEmpty(ServiceMethod))
                {

                    string[] service = ServiceMethod.Split('.');
                    for (int i = 0; i < service.Length - 1; i++)
                    {
                        bizClass += service[i] + ".";
                    }
                    if (bizClass.EndsWith("."))
                        bizClass = bizClass.Substring(0, bizClass.Length - 1);
                }
                return bizClass;
            }
        }

        protected string bizServiceMethod
        {
            get
            {
                string bizMethod = "";
                if (!string.IsNullOrEmpty(ServiceMethod))
                {

                    string[] service = ServiceMethod.Split('.');
                    bizMethod += service[service.Length - 1];

                }
                return bizMethod;
            }
        }

        protected IList InvokeServiceMethod(string serviceMethod)
        {
            Assembly assembly = Assembly.Load("ZebraCoreLib");
            Type bizServiceType = assembly.GetType(bizServiceClass, false, true);
            object bizService = Activator.CreateInstance(bizServiceType);
           
            int pageSize = Pager.PageSize;
            int pageIndex=1;
            ListFlag = ListFlags.Details;
            MethodInfo method = bizServiceType.GetMethod(bizServiceMethod,new Type[]{typeof(string),typeof(ListFlags),typeof(int),typeof(int),typeof(int).MakeByRefType()});

            object pageCount = 0; //sucode='its00002'
            string  parameters="1=1";
            if (html.ViewData["queryParameters"] != null)
                parameters = html.ViewData["queryParameters"].ToString();
            if (html.ViewData["pageindex"] != null)
            {
                try
                {
                    pageIndex = int.Parse(html.ViewData["pageindex"].ToString());
                }
                catch { }

            }
            Pager.PageIndex = pageIndex;
           


            object[] objParameters = new object[] { parameters, ListFlag, pageIndex, pageSize, pageCount };
            IList result = method.Invoke(bizService, objParameters) as IList;
            Pager.PageCount = (int)objParameters[4];

            Pager.PrevPage = Pager.PageIndex - 1;
            if (Pager.PageIndex == 1)
                Pager.PrevPage = 0;

            Pager.NextPage = Pager.PageIndex + 1;
            if (Pager.NextPage == Pager.PageCount)
                Pager.NextPage = Pager.PageCount;

            return result;
        }

        public override void RenderControl(HtmlTextWriter writer)
        {
          
            RenderHeader(writer);
            RenderListItem(writer);
            RenderFooter(writer);
        }

        protected virtual void RenderHeader(HtmlTextWriter writer)
        {
            RenderControlFromUserControls(writer, UserControlFileName, TemplatePart.Header);
        }
        protected virtual void RenderListItem(HtmlTextWriter writer)
        {
            IList bizServices = InvokeServiceMethod(ServiceMethod);
           
            foreach (var bizService in bizServices)
            {
                Hashtable item = new Hashtable();
                var properties=bizService.GetType().GetProperties( BindingFlags.Instance | BindingFlags.Public);
                foreach (var property in properties)
                {
                    item[property.Name] = property.GetValue(bizService, null)==null?"":property.GetValue(bizService, null);
                }

                RenderControlFromUserControls(bizService, UserControlFileName, TemplatePart.ListItem);
            }
        }


        public string GetPropertyValue(object bizService, string property)
        {
            var p = bizService.GetType().GetProperty(property, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
            return p.GetValue(bizService, null).ToString();
        }

        protected virtual void RenderFooter(HtmlTextWriter writer)
        {
            RenderControlFromUserControls(writer, UserControlFileName, TemplatePart.Footer);
        }

    }
}